package com.dlc.apk.utils

import android.app.Service
import android.os.Vibrator
import android.view.View
import android.view.animation.Animation
import android.view.animation.CycleInterpolator
import android.view.animation.TranslateAnimation
import com.dlc.apk.base.BaseApp


class ViewShakeHelper private constructor() {
    // 震动动画
    private val shakeAnimation: Animation

    // 插值器
    private val cycleInterpolator: CycleInterpolator

    // 振动器
    private var shakeVibrator: Vibrator? = null

    private object InstanceHolder {
        val instance = ViewShakeHelper()
    }

    /**
     * 开始震动
     *
     * @param views
     */
    fun shake(vararg views: View) {
        for (v in views) {
            v.startAnimation(shakeAnimation)
        }
        //shakeVibrator?.vibrate(longArrayOf(0L, 500L), -1)
    }

    companion object {
        fun getInstance(): ViewShakeHelper {
            return InstanceHolder.instance
        }
    }

    init {
        // 初始化振动器
        shakeVibrator = BaseApp.instance.getSystemService(Service.VIBRATOR_SERVICE) as Vibrator
        // 初始化震动动画
        shakeAnimation = TranslateAnimation(0f, 10f, 0f, 0f)
        shakeAnimation.duration = 200
        cycleInterpolator = CycleInterpolator(8f)
        shakeAnimation.interpolator = cycleInterpolator
    }
}